use warnings;
use strict;

use FindBin;
use lib "$FindBin::Bin/../t/lib";

use Test::More qw/no_plan/;
use Test::Exception;
use Test::Askgene::File;

BEGIN {
    use_ok('Askgene::File');
}

isa_ok( my $file = Askgene::File->new(), 'Askgene::File' );
ok( !$file->exists, q{doesn't create file on new()} );
ok( !defined $file->as_string,
    q{stringifies to undefined when path isn't set} );

throws_ok { $file->create } qr/can't create file without a filename/,
    'create throws exception when attempting to create without a path defined';
$file->path( [qw|tmp foo|] );
ok( $file->create,   'creates file' );
ok( $file->exists,   '...correctly' );
ok( !$file->create,  q{doesn't create the file a second time} );
ok( !$file->is_temp, q{files not temporary by default} );

is( $file, $file->as_string, 'stringification overloads to as_string' );

fake_file_delete();
ok( !$file->exists, 'reports file deletion correctly' );

my $data = "hello world!\n";
$file->set_data($data);
is( $file->slurp, $data, 'reads/writes data from/to file' );

fake_bad_write();
throws_ok { $file->set_data($data) } qr/couldn't write to file '$file':/,
    'set_data() throws exception when file write fails';

$file->is_temp(1);
undef $file;
file_removed_ok('removes temporary files');

$file = Askgene::File->new();
$file->path( [qw|tmp foo|] );
ok($file->create);
file_not_removed_ok();
